/*
 * @Author: shenya shenya0203@163.com
 * @Date: 2024-03-11 14:39:56
 * @LastEditors: shenya shenya0203@163.com
 * @LastEditTime: 2024-04-15 13:44:48
 * @FilePath: /code/include/json_data.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef __JSON_DATA_C
#define __JSON_DATA_C

#include <stdint.h>
#include <cJSON.h>

typedef struct
{
    uint8_t *str1;
    uint8_t *str2;
    /* data */
} DOUSTR;

// json处理结果
enum ErrorJsonCode
{
    ERROR_NONE = 0,      // 没有错误
    ERROR_INVALID_PARAM, // 参数无效
    ERROR_OUT_OF_MEMORY, // 内存出错
    ERROR_JSON_NULL,     // json为空
    ERROR_NOT_ID,        // 没有查找项id
    ERROR_NOT_ATTR,      // 没有查找属性
};

extern uint8_t *attributeName_report[];
uint16_t attributeName_report_SIZEfunc();  

extern uint8_t *devList_p;    // 设备列表指针
extern cJSON *devList_Json;   // 设备列表JSON对象
extern uint8_t *sceneList_p;  // 场景列表指针
extern cJSON *sceneList_Json; // 场景列表JSON对象
extern uint8_t *groupList_p;  // 群组列表指针
extern cJSON *groupList_Json; // 群组列表JSON对象
extern uint8_t *roomList_p;  // 房间列表指针
extern cJSON *roomList_Json; // 房间列表JSON对象

char *replace_string(const char *input, const char *pattern, const char *replacement);
char *format_string(const char *format, ...);
char *strdup_custom(const char *src);
int cJSON_setReplaceNumberValue(cJSON *obj, cJSON *child, int value);

char* attributeToSerName(char *msg);
char* attributeToMqttName(char *msg);
int attributeName_rename(uint8_t *cli_name, uint8_t **ser_name);
int devAttribute_rename(uint8_t *cli_name, uint8_t *ser_name);
int jsonValueToSerStr(cJSON *json);
int jsonValueToMqttStr(cJSON *json);
void jsonValueToSerIteration(cJSON *item);
void jsonValueToMqttIteration(cJSON *item);

void devList_get(uint8_t *text);
int listJson_get(uint8_t *text, uint8_t **listText_p, cJSON **root);
int devList_getAttribute(uint8_t *devID, uint8_t *attr, uint8_t *value);
int sceneList_getAttributeJson(uint8_t *devID, cJSON **attr);

void checkJson(uint8_t *devID, cJSON *json);
int check_sceneJson(uint8_t *devID, cJSON *json);

int check_groupJson(uint8_t *devID, cJSON *json);
int scencJson_newValue(uint8_t *id, cJSON **objectJson);

int check_groupJson(uint8_t *devID, cJSON *json);
int groupJson_newValue(uint8_t *id, cJSON **objectJson);

int checkRoomJson(uint8_t *devID, cJSON *json);

int attribute_rename(uint8_t *cli_name, uint8_t *ser_name);
int list_getAttributeJson(cJSON *list_Json, uint8_t *index, uint8_t *devID, cJSON **attr);
int listJson_newValue(cJSON *list_Json, uint8_t *index, uint8_t *id, cJSON **objectJson);
int check_listJson(cJSON *list_Json, uint8_t *index, uint8_t *devID, cJSON *json);

int list_getItemJson(cJSON *list_Json, cJSON *index, cJSON **item);
int list_getOneAttrJson(cJSON *list_Json, cJSON *index, cJSON **result);
int list_update_stateData(uint8_t *data, uint16_t dataLen, uint16_t addr);

int list_getDeviceType(uint8_t *id, uint16_t *deviceType);
int list_getShortAddr(uint8_t *id, uint16_t *addr);

int checkDevJson(uint8_t *devID, cJSON *json);
int checkSceneJson(uint8_t *devID, cJSON *json);
int checkGroupJson(uint8_t *devID, cJSON *json);
int checkReportJson(uint8_t *devID, cJSON *json);

int controlDevJson(uint8_t *devID, cJSON *json);
//controlSceneJson直接执行
int controlGroupJson(uint16_t groupID, cJSON *json);
int controlRoomJson(uint16_t roomID, cJSON *json);
int controlAllJson(uint16_t ID, cJSON *json);

int matchItemJson(cJSON *index, cJSON *item);
int updateItemJson(cJSON *index, cJSON *item);
int hz_list_device_get(char *id, cJSON *filter, cJSON *config);
int hz_list_scene_get(char *id, cJSON *filter, cJSON *config);
int hz_list_group_get(char *id, cJSON *filter, cJSON *config);
int hz_list_room_get(char *id, cJSON *filter, cJSON *config);

#endif